﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

namespace Web.myadmin
{
    public partial class fanli_shopComment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //SessionHelp.AssertLogin(this); //登录验证

            #region 显示正确的布局
            string cmd = Request.QueryString["cmd"];
            switch (cmd)
            {
                case "add": initAddForm();break;
                case "edit":initEditFrom();break;
                case "view": initViewTable(); break;
                case "delete": delete(); break;
                default: showInfoList(); break;
            }
            showTheRightRegion(cmd);
            #endregion
        }

        #region 信息列表和查询
        /// <summary>
        /// 查询数据
        /// </summary>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            showInfoList();
        }

        /// <summary>
        /// 数据列表
        /// </summary>
        protected void showInfoList()
        {
            #region 变量定义
            DataSet dset = new DataSet();
            fanli.DAL.fanli_shopComment dal = new fanli.DAL.fanli_shopComment();
            System.Collections.Generic.List<System.Data.SqlClient.SqlParameter> paraList = new List<System.Data.SqlClient.SqlParameter>();
            string dataFields = ""; //读取的字段名，空为所有
            string strWhere = "1=1"; //查询子句
            string strOrderBy = " id desc"; //排序参数，如果需要分页，则必填 
            #endregion

            #region 查询条件
            string strId = txtQryId.Text.Trim();
            if (strId != "" && strId != null )
            {
                strWhere += " and id=@id";
                paraList.Add(new System.Data.SqlClient.SqlParameter("@id", strId));
            } 
            string strShopid = txtQryShopid.Text.Trim();
            if (strShopid != "" && strShopid != null )
            {
                strWhere += " and shopid=@shopid";
                paraList.Add(new System.Data.SqlClient.SqlParameter("@shopid", strShopid));
            } 
            //string strUserid = txtQryUserid.Text.Trim();
            //if (strUserid != "" && strUserid != null )
            //{
            //    strWhere += " and userid=@userid";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@userid", strUserid));
            //} 
            //string strUsername = txtQryUsername.Text.Trim();
            //if (strUsername != "" && strUsername != null )
            //{
            //    strWhere += " and username=@username";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@username", strUsername));
            //} 
            //string strTitle = txtQryTitle.Text.Trim();
            //if (strTitle != "" && strTitle != null )
            //{
            //    strWhere += " and title=@title";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@title", strTitle));
            //} 
            //string strContent = txtQryContent.Text.Trim();
            //if (strContent != "" && strContent != null )
            //{
            //    strWhere += " and content=@content";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@content", strContent));
            //} 
            //string strScore1 = txtQryScore1.Text.Trim();
            //if (strScore1 != "" && strScore1 != null )
            //{
            //    strWhere += " and score1=@score1";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@score1", strScore1));
            //} 
            //string strScore2 = txtQryScore2.Text.Trim();
            //if (strScore2 != "" && strScore2 != null )
            //{
            //    strWhere += " and score2=@score2";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@score2", strScore2));
            //} 
            //string strScore3 = txtQryScore3.Text.Trim();
            //if (strScore3 != "" && strScore3 != null )
            //{
            //    strWhere += " and score3=@score3";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@score3", strScore3));
            //} 
            //string strScore4 = txtQryScore4.Text.Trim();
            //if (strScore4 != "" && strScore4 != null )
            //{
            //    strWhere += " and score4=@score4";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@score4", strScore4));
            //} 
            //string strPubTime = txtQryPubTime.Text.Trim();
            //if (strPubTime != "" && strPubTime != null )
            //{
            //    strWhere += " and pubTime=@pubTime";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@pubTime", strPubTime));
            //} 
            //string strPubIP = txtQryPubIP.Text.Trim();
            //if (strPubIP != "" && strPubIP != null )
            //{
            //    strWhere += " and pubIP=@pubIP";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@pubIP", strPubIP));
            //} 
            //string strStatus = txtQryStatus.Text.Trim();
            //if (strStatus != "" && strStatus != null )
            //{
            //    strWhere += " and status=@status";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@status", strStatus));
            //} 
            //string strExt1 = txtQryExt1.Text.Trim();
            //if (strExt1 != "" && strExt1 != null )
            //{
            //    strWhere += " and ext1=@ext1";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@ext1", strExt1));
            //} 
            //string strExt2 = txtQryExt2.Text.Trim();
            //if (strExt2 != "" && strExt2 != null )
            //{
            //    strWhere += " and ext2=@ext2";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@ext2", strExt2));
            //} 
            #endregion

             #region 分页控件绑定
             int pageSize = 30; //分页每页记录个数
             int totalRecourd = 0; //总记录个数
             int currentPage = 1; //当前页码
             try { currentPage = int.Parse(Request.QueryString["p"]); }
             catch { }

             totalRecourd = dal.GetCount(strWhere, paraList);
             dset = dal.GetDatasetByPageSql(currentPage, pageSize, strWhere, strOrderBy, paraList, dataFields);
             if (dset!=null && dset.Tables[0].Rows.Count > 0)
             {
                 rptInfoList.DataSource = dset;
                 rptInfoList.DataBind();
                 litPager.Text = LIB.pager.getPageHtml(this, totalRecourd, pageSize);
             }
             else
             {
                 litPromptMessage.Text = "没有查询到相关记录";
             } 
             #endregion
             #region 单页数据绑定
             //strWhere += " " + strOrderBy;
             //dset = dal.GetDataset(strWhere, paraList, dataFields);
             //if (dset.Tables[0].Rows.Count > 0)
             //{
             //    rptInfoList.DataSource = dset;
             //    rptInfoList.DataBind();
             //}
             //else
             //{
             //    litPromptMessage.Text = "没有查询到相关记录";
             //} 
             #endregion

        }
        #endregion

        #region 添加、修改、删除操作
        /// <summary>
        /// 添加修改记录
        /// </summary>
        protected void btnSumit_Click(object sender, EventArgs e)
        {
            #region 获取数据
            bool isAdd = true;
            int varId ;	//id
            int varShopid ;	//商城id
            int varUserid ;	//用户id
            string varUsername;	//用户名
            string varTitle;	//标题
            string varContent;	//内容
            int varScore1 ;	//评分1
            int varScore2 ;	//评分2
            int varScore3 ;	//评分3
            int varScore4 ;	//评分4
            DateTime varPubTime ;	//发布时间
            string varPubIP;	//发布ip
            int varStatus ;	//状态
            string varExt1;	//扩展1
            string varExt2;	//扩展2
            try
            {
               varId =int.Parse(txtId.Text.Trim());	//id
               varShopid =int.Parse(txtShopid.Text.Trim());	//商城id
               varUserid =int.Parse(txtUserid.Text.Trim());	//用户id
               varUsername =txtUsername.Text.Trim();	//用户名
               varTitle =txtTitle.Text.Trim();	//标题
               varContent =txtContent.Text.Trim();	//内容
               varScore1 =int.Parse(txtScore1.Text.Trim());	//评分1
               varScore2 =int.Parse(txtScore2.Text.Trim());	//评分2
               varScore3 =int.Parse(txtScore3.Text.Trim());	//评分3
               varScore4 =int.Parse(txtScore4.Text.Trim());	//评分4
               varPubTime =DateTime.Parse(txtPubTime.Text.Trim());	//发布时间
               varPubIP =txtPubIP.Text.Trim();	//发布ip
               varStatus =int.Parse(txtStatus.Text.Trim());	//状态
               varExt1 =txtExt1.Text.Trim();	//扩展1
               varExt2 =txtExt2.Text.Trim();	//扩展2
            }
            catch
            {
               LIB.JsWrite.Alert(this, "输入数据格式不合法，请检查输入数据。");
               return;
            }
            if (varId != 0) isAdd = false;
            #endregion

            #region 赋值model
            fanli.DAL.fanli_shopComment dal = new fanli.DAL.fanli_shopComment();
            fanli.Model.fanli_shopComment model = new fanli.Model.fanli_shopComment();
            if (!isAdd) model = dal.GetModel(varId);
            model.id = varId;	//id
            model.shopid = varShopid;	//商城id
            model.userid = varUserid;	//用户id
            model.username = varUsername;	//用户名
            model.title = varTitle;	//标题
            model.content = varContent;	//内容
            model.score1 = varScore1;	//评分1
            model.score2 = varScore2;	//评分2
            model.score3 = varScore3;	//评分3
            model.score4 = varScore4;	//评分4
            model.pubTime = varPubTime;	//发布时间
            model.pubIP = varPubIP;	//发布ip
            model.status = varStatus;	//状态
            model.ext1 = varExt1;	//扩展1
            model.ext2 = varExt2;	//扩展2
            #endregion

            #region 执行操作
            if (isAdd)
            {
                if (dal.Add(model) > 0)
                {
                    LIB.JsWrite.AlertAndGo(this, "添加成功", "?cmd=list");
                }
                else
                {
                    LIB.JsWrite.AlertAndGo(this, "操作失败", "?cmd=list");
                }
            }
            else
            {
                if (dal.Update(model) > 0)
                {
                    LIB.JsWrite.Alert(this, "修改成功");
                }
                else
                {
                    LIB.JsWrite.Alert(this, "操作失败");
                }
            } 
            #endregion
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        protected void delete()
        {
            #region 删除操作
            int id = 0;
            try { id = int.Parse(Request.QueryString["id"]); }
            catch
            {
                //litPromptMessage.Text = "非法的参数";
                LIB.JsWrite.Alert(this, "非法的参数");
                return;
            }

            fanli.DAL.fanli_shopComment dal = new fanli.DAL.fanli_shopComment();
            if (dal.Delete(id)>0)
            {
                //litPromptMessage.Text = "删除成功";
                LIB.JsWrite.AlertAndGo(this, "删除成功", "?cmd=list");
            }
            else
            {
                //litPromptMessage.Text = "操作失败";
                LIB.JsWrite.AlertAndGo(this, "操作失败", "?cmd=list");
            } 
            #endregion
        }
        #endregion

        #region 初始化添加表单
        /// <summary>
        /// 初始化添加表单
        /// </summary>
        protected void initAddForm()
        {
            lblFormTitle.Text = "添加信息";
            if (!IsPostBack)
            {

                txtShopid.Text = "";
                txtUserid.Text = "";
                txtUsername.Text = "";
                txtTitle.Text = "";
                txtContent.Text = "";
                txtScore1.Text = "";
                txtScore2.Text = "";
                txtScore3.Text = "";
                txtScore4.Text = "";
                txtPubTime.Text = "";
                txtPubIP.Text = "";
                txtStatus.Text = "";
                txtExt1.Text = "";
                txtExt2.Text = "";
            }
        }
        #endregion

        #region 初始化修改表单
        /// <summary>
        /// 初始化修改表单
        /// </summary>
        protected void initEditFrom()
        {
            int id =Convert.ToInt32(Request.QueryString["id"]);
            lblFormTitle.Text = "修改信息";
            if (!IsPostBack)
            {
                fanli.Model.fanli_shopComment model = new fanli.Model.fanli_shopComment();
                fanli.DAL.fanli_shopComment dal = new fanli.DAL.fanli_shopComment();
                model = dal.GetModel(id);

                txtId.Text = model.id.ToString();
                txtShopid.Text = model.shopid.ToString();
                txtUserid.Text = model.userid.ToString();
                txtUsername.Text = model.username.ToString();
                txtTitle.Text = model.title.ToString();
                txtContent.Text = model.content.ToString();
                txtScore1.Text = model.score1.ToString();
                txtScore2.Text = model.score2.ToString();
                txtScore3.Text = model.score3.ToString();
                txtScore4.Text = model.score4.ToString();
                txtPubTime.Text = model.pubTime.ToString();
                txtPubIP.Text = model.pubIP.ToString();
                txtStatus.Text = model.status.ToString();
                txtExt1.Text = model.ext1.ToString();
                txtExt2.Text = model.ext2.ToString();
            }
        }
        #endregion

        #region 初始化查看表格
        /// <summary>
        /// 初始化查看表格
        /// </summary>
        protected void initViewTable()
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            lblViewTitle.Text = "查看信息";
            if (!IsPostBack)
            {
                fanli.Model.fanli_shopComment model = new fanli.Model.fanli_shopComment();
                fanli.DAL.fanli_shopComment dal = new fanli.DAL.fanli_shopComment();
                model = dal.GetModel(id);

                lblShopid.Text = model.shopid.ToString();
                lblUserid.Text = model.userid.ToString();
                lblUsername.Text = model.username.ToString();
                lblTitle.Text = model.title.ToString();
                lblContent.Text = model.content.ToString();
                lblScore1.Text = model.score1.ToString();
                lblScore2.Text = model.score2.ToString();
                lblScore3.Text = model.score3.ToString();
                lblScore4.Text = model.score4.ToString();
                lblPubTime.Text = model.pubTime.ToString();
                lblPubIP.Text = model.pubIP.ToString();
                lblStatus.Text = model.status.ToString();
                lblExt1.Text = model.ext1.ToString();
                lblExt2.Text = model.ext2.ToString();
            }
        }

        #endregion

        #region 自定义函数

        /// <summary>
        /// 函数功能：示例函数
        /// 作者：buzhizhe 创建时间：2011.6.22
        /// 修改者：buzhizhe 最后修改时间：2011.6.22
        /// </summary>
        protected void YourFunction()
        {
            ///
        }


        #endregion


        #region 公共函数，不需要修改

        /// <summary>
        /// 根据cmd参数判断应该显示哪些部分内容
        /// </summary>
        /// <param name="cmd"></param>
        protected void showTheRightRegion(string cmd)
        {
            #region show or hide div
            switch (cmd)
            {
                case "add":
                    divView.Visible = false;
                    divForm.Visible = true;
                    divList.Visible = false;
                    divQuery.Visible = false;
                    break;
                case "edit":
                    divView.Visible = false;
                    divForm.Visible = true;
                    divList.Visible = false;
                    divQuery.Visible = false;
                    break;
                case "view":
                    divView.Visible = true;
                    divForm.Visible = false;
                    divList.Visible = false;
                    divQuery.Visible = false;
                    break;
                case "delete":
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    break;
                case "query":
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    divQuery.Visible = true;
                    break;
                default:
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    divQuery.Visible = true;
                    break;
            }
            #endregion
        }
        #endregion



    }


}

